export default class Local_Window_TypeSelect extends Window_ItemCategory {
    constructor(...list) {
        super(...list)
        this._windowCursorSprite.opacity = 0;
        // 新增背景
        this._backgroundSprite = new Sprite();
        this._backgroundSprite.bitmap =
            ImageManager.loadBitmap('img/system/UI/', 'MenuBg_Category');
        this.addChildAt(this._backgroundSprite, 0);
        this._backgroundSprite.x = this.standardPadding() - 9;
        this._backgroundSprite.y = this.standardPadding() - 7;
    }
    maxCols() {
        return 5   //5
    }
    maxItems() {
        return CP.SynthesisManager.UI_TYPE_COMMANDS.length;
    };
    windowWidth() {
        return 500
    }

    defaultTextColorCode() {
        return 0
    }

    selectTextColorCode() {
        return 6
    }

    drawColorItem(index, colorCode = 0) {
        const rect = this.itemRectForText(index);
        const align = this.itemTextAlign();
        this.changeTextColor(this.textColor(colorCode)) // 使用自定义颜色，而不是使用黑色

        this.changePaintOpacity(this.isCommandEnabled(index));

        const command = CP.SynthesisManager.UI_TYPE_COMMANDS[index];

        this.drawText(command.name, rect.x, rect.y, rect.width, align);
    }

    drawAllItems() {
        const curIndex = this.index()

        const topIndex = this.topIndex();
        for (let i = 0; i < this.maxPageItems(); i++) {
            const index = topIndex + i;
            if (index < this.maxItems()) {
                const colorCode = index === curIndex ? this.selectTextColorCode() : this.defaultTextColorCode()
                this.drawColorItem(index, colorCode);
            }
        }
    }

    updateAllItems() {
        if (this.contents) {
            this.contents.clear()
            this.drawAllItems()
        }
    }

    processOk() {
    }

    setItemWindow(itemWindow) {
        super.setItemWindow(itemWindow)

        //this.select(0);【饕2023：似乎没用，但对商店界面有影响】

    };
    select(index) {
        super.select(index)
        this.updateAllItems() // 选择时更新字体颜色
        if (this._itemWindow){
           //category窗口的_currentExt刷新（update中最后一步）在select之后，
           //需在此事先setExt一下，否则无法随select根据category所选类别刷新列表。
            // 两个必须都有。setItemType决定_data的刷新。
            this._itemWindow.setItemType(this.index());
            this._itemWindow.setExt(this.currentExt());
            this._itemWindow.refresh();
            this._itemWindow.selectLast();
            this._itemWindow.showView()
        }

        if(!!this._onSelectMethod)
            this._onSelectMethod.call(this);
    }


   setOnSelectMethod(method) {
        this._onSelectMethod = method;
    };

    selectLast() {
        const item = $gameParty.lastSynthesisItem();
        if (item && ConfigManager.commandRemember) {
            this.selectExtbyItem(item);
            if(!!this._itemWindow){
                // 两个必须都有。setItemType决定_data的刷新。
                this._itemWindow.setItemType(this.index());
                this._itemWindow.setCategory(this.currentSymbol());
                this._itemWindow.refresh();
                this._itemWindow.selectLast();
            }
        } else {
            this.select(0);
            if(!!this._itemWindow){
                this._itemWindow.refresh();
                this._itemWindow.select(0);
            }
        }
    };

    // 修复类别选择错误问题（不再使用原有的根据物品category判断类型的selectExt）
    // 实际用的是：Window_SynthesisItemList.prototype.includes。应该无伤大雅
    selectExtbyItem(item) {
        let index = -1;
        index = this.itemTypeId(item);
        if (index >= 0) {
            this.select(index);
        } else {
            this.select(0);
        }
    };

    // 修复类别选择错误问题：根据插件里的规则判断物品是否属于该类型
    itemTypeId(item){
        if (!item) return -1;
        if (DataManager.isItem(item) && item.id < 120) return 0;
        if (DataManager.isWeapon(item) || (DataManager.isArmor(item) && !item.itemCategory.contains("护驾"))) return 1;
        if (item.itemCategory.contains("护驾")) return 2;
        return -1;
    };
    
    
}